package at.fhooe.mcm12.java.sensors;

import java.rmi.RemoteException;
import java.util.Date;
import java.util.Random;
import at.fhooe.mcm12.java.enums.OutputFormat;
import at.fhooe.mcm12.java.sensors.data.AirPressure;
import at.fhooe.mcm12.java.sensors.data.SensorValue;
import at.fhooe.mcm12.java.serializer.PressureSerializer;

public class PressureSensor extends PollSensor{

	private Random m_pressureGenerator;
	
	public PressureSensor() throws RemoteException
	{
		super();
		Date dt = new Date();
		
		m_dataType = new AirPressure(0,dt.getTime(),"Pa");
		m_pressureGenerator = new Random();
		m_serializer = new PressureSerializer();
		m_isSubscribable = true;
		
		m_description = "Provides information about the currently measured pressure in Pascal.";
		Thread t = new Thread(this);
		t.start();
	}
	
	@Override
	public SensorValue getSensorValue() {
		serialize(OutputFormat.XML, m_dataType);
		serialize(OutputFormat.CSV, m_dataType);
		serialize(OutputFormat.Text, m_dataType);
		
		return m_dataType;
	}

	@Override
	public void refreshSensorValue() {
		m_dataType.setTimestamp(new Date().getTime());
		((AirPressure)m_dataType).setPressure(1100 + (m_pressureGenerator.nextGaussian() * 50));
	}

	@Override
	public void serialize(OutputFormat _format, SensorValue _value) {
		m_serializer.serialize(_format, _value);
	}
}
